//
//  XIBVC.m
//  SYPerfectDemo
//
//  Created by Lssyyy on 2018/10/24.
//  Copyright © 2018年 Lssyyy. All rights reserved.
//

#import "XIBVC.h"
#import "XIBView.h"
#import "SDWebImageManager.h"
#import "SDImageCache.h"
@interface XIBVC ()
@property (nonatomic,strong) XIBView *xibView;
@property (nonatomic,strong) NSTimer *timer;
@end

@implementation XIBVC
-(UIImage*)imageFromSdcache:imageUrlStr{
    NSLog(@"开始查询");
    __block NSData*imageData =nil;
    [[SDWebImageManager sharedManager]diskImageExistsForURL:[NSURL URLWithString:imageUrlStr] completion:^(BOOL isInCache) {
        if (isInCache) {
            NSString*cacheImageKey = [[SDWebImageManager sharedManager]cacheKeyForURL:[NSURL URLWithString:imageUrlStr]];
            if(cacheImageKey.length) {
                NSString*cacheImagePath = [[SDImageCache sharedImageCache]defaultCachePathForKey:cacheImageKey];
                if(cacheImagePath.length) {
                    imageData = [NSData dataWithContentsOfFile:cacheImagePath];
                }
            }
            if(!imageData) {
                imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrlStr]];
            }
        }
        NSLog(@"查询完毕");
    }];
    UIImage*image = [UIImage imageWithData:imageData];
    NSLog(@"结束查询");
    return image;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.timer =  [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timerGo) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
    
    UIImage *image = [self imageFromSdcache:@"123"];
}
- (void)timerGo{
    NSLog(@"时间走");
}
- (void)addUI{
    [self.view addSubview:self.xibView];
}

- (XIBView *)xibView{
    if (!_xibView) {
        _xibView = [[XIBView alloc]init];
        _xibView.frame = self.view.frame;
    }
    return _xibView;
}

@end
